package cn.lili.modules.order.aftersale.serviceimpl;

import cn.hutool.core.text.CharSequenceUtil;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.CurrencyUtil;
import cn.lili.modules.order.aftersale.entity.dos.AfterSale;
import cn.lili.modules.order.aftersale.mapper.AfterSaleStatisticsMapper;
import cn.lili.modules.order.aftersale.service.AfterSaleStatisticsService;
import cn.lili.modules.order.trade.entity.enums.AfterSaleStatusEnum;
import cn.lili.modules.statistics.entity.dto.StatisticsQueryParam;
import cn.lili.modules.statistics.entity.vo.OrderOverviewVO;
import cn.lili.modules.statistics.util.StatisticsDateUtil;
import cn.lili.modules.store.client.StoreClient;
import cn.lili.modules.store.entity.dos.Store;
import cn.lili.mybatis.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 售后统计业务层实现
 *
 * @author Bulbasaur
 * @since 2020/12/9 11:30
 */
@Service
@RequiredArgsConstructor
public class AfterSaleStatisticsServiceImpl extends ServiceImpl<AfterSaleStatisticsMapper, AfterSale> implements AfterSaleStatisticsService {

    private final StoreClient storeClient;

    @Override
    public long applyNum(String serviceType, String storeId) {
        LambdaQueryWrapper<AfterSale> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AfterSale::getServiceStatus, AfterSaleStatusEnum.APPLY.name());
        queryWrapper.eq(CharSequenceUtil.isNotEmpty(serviceType), AfterSale::getServiceType, serviceType);
        queryWrapper.eq(CharSequenceUtil.isNotEmpty(storeId), AfterSale::getStoreId, storeId);
        return this.count(queryWrapper);
    }


    @Override
    public Page<AfterSale> getStatistics(StatisticsQueryParam statisticsQueryParam) {

        LambdaQueryWrapper<AfterSale> queryWrapper = new LambdaQueryWrapper<>();
        Date[] dates = StatisticsDateUtil.getDateArray(statisticsQueryParam);
        queryWrapper.between(AfterSale::getCreateTime, dates[0], dates[1]);
        queryWrapper.eq(CharSequenceUtil.isNotEmpty(statisticsQueryParam.getStoreId()), AfterSale::getStoreId, statisticsQueryParam.getStoreId());

        return this.page(PageUtil.initPage(statisticsQueryParam), queryWrapper);
    }

    @Override
    public long applyNumByUrban(String serviceType, String storeId) {
        LambdaQueryWrapper<AfterSale> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AfterSale::getServiceStatus, AfterSaleStatusEnum.APPLY.name());
        queryWrapper.eq(CharSequenceUtil.isNotEmpty(serviceType), AfterSale::getServiceType, serviceType);
        List<Store> storeList = storeClient.getSupplierListByUrbanId(UserContext.getCurrentUser().getId());
        if (!storeList.isEmpty()) {
            List<String> ids = storeList.stream().map(Store::getId).toList();
            queryWrapper.in(AfterSale::getSupplierId, ids);
        }else {
            queryWrapper.eq(AfterSale::getSupplierId, "");
        }

        return this.count(queryWrapper);
    }

    @Override
    public void initAfterSale(Date[] dates, OrderOverviewVO orderOverviewVO, StatisticsQueryParam statisticsQueryParam) {
        //付款订单数，付款金额
        QueryWrapper<AfterSale> queryWrapper = Wrappers.query();
        queryWrapper.between("create_time", dates[0], dates[1]);
        queryWrapper.select("SUM(flow_price) AS price , COUNT(0) AS num");
        //如果有店铺id传入，则查询店铺
        if (CharSequenceUtil.isNotEmpty(statisticsQueryParam.getStoreId())) {
            queryWrapper.eq("store_id", statisticsQueryParam.getStoreId());
        }
        queryWrapper.gt("flow_price", 0);
        queryWrapper.eq("service_status", AfterSaleStatusEnum.COMPLETE.name());
        Map payment = this.getMap(queryWrapper);
        orderOverviewVO.setRefundOrderNum(payment != null && payment.containsKey("num") ? (Long) payment.get("num") : 0L);
        orderOverviewVO.setRefundOrderPrice(payment != null && payment.containsKey("price") ?
                CurrencyUtil.convertDouble(payment.get("price").toString()) : 0D);
    }
}
